/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.ConnectData;
import DTO.Category3;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Vien-IT
 */
public class BCategory3 {
    
    private ConnectData condata;

    public BCategory3() {
        condata = new ConnectData();
    }
    
    public ArrayList<Category3> getAll(String sql)
    {
        ArrayList<Category3> categories3 = new ArrayList<Category3>();
        Category3 category3 = null;
        try {
            ResultSet rs = condata.executeQuery(sql);
            while(rs.next())
            {
                category3 = new Category3();
                category3.setCategory3Id(rs.getInt("category3Id"));
                category3.setName(rs.getString("name"));
                category3.setCategory2Id(rs.getInt("category2Id"));
                categories3.add(category3);
                
            }
            condata.close();
        } catch (Exception e) {
        }
        
        return categories3.isEmpty() ? null : categories3;
    }
    
    public ArrayList<Category3> getAll()
    {
        String sql = "select * from tblCategory3";
           return getAll(sql);
    }
    
    
    public Category3 getById(int category3)
    {
        String sql = "select * from tblCategory3 where category3Id = "+category3+"";
        ArrayList<Category3> categories3 = getAll(sql);
        return categories3 == null ? null : categories3.get(0);
    }
    
    
    public ArrayList<Category3> getByCategory2Id(int category2Id)
    {
        String sql = "select * from tblCategory3 where category2Id = "+category2Id+"";
        return this.getAll(sql);
    }
    
    public boolean insert(Category3 category3)
    {
        boolean check = false;
        String sql = "insert into tblCategory3 (name) values (N'"+category3.getName()+"')";
        int rs = -1;
        try {
            rs = condata.executeUpdate(sql);
            if(rs > 0)
                check = true;
        } catch (Exception e) {
        }
        return check ;
    }
    
}
